package com.chen.lock;

// 传统的synchronized锁
public class SaleTicketDemo01 {
    public static void main(String[] args) {

        Ticket ticket = new Ticket();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sales();
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sales();
            }
        }, "B").start();

        new Thread(() -> {
            for (int i = 0; i < 40; i++) {
                ticket.sales();
            }
        }, "C").start();

    }
}

// 资源类
class Ticket {
    private int number = 30;

    public synchronized void sales() {
        if (number > 0) {
            System.out.println(Thread.currentThread().getName() + "卖出了一张票， 剩余：" + number--);
        }
    }
}
